﻿using System.Collections.ObjectModel;
using System.Windows;
using JulMar.Silverlight.Mvvm;
using MediaBrowserSL.RIA.MvvmUtils;
using MediaBrowserSL.RIA.MvvmUtils.UI;
using MediaBrowserSL.RIA.MediaBrowserServiceReference;
using JulMar.Silverlight.Extensions;
using MediaBrowserSL.RIA.Services;

namespace MediaBrowserSL.RIA.ViewModels
{
    /// <summary>
    /// Main View Model
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        #region Private Member
        private readonly ObservableCollection<PageViewModelBase> views;
        private PageViewModelBase currentContent;
        private bool isGoForwardView;
        private bool isFullScreenCommandVisible = true;
        private IWindowAboutVisualizer windowAboutVisualizer;
        private IAuthenticationVisualizer authenticationVisualizer;
        private IWindowSettingsVisualizer windowSettingsVisualizer;
        private bool viewLogOut;
        #endregion

        #region Public properties
        public PageViewModelBase CurrentContent
        {
            get
            {
                return this.currentContent;
            }
            private set
            {
                this.currentContent = value;
                OnPropertyChanged("CurrentContent");
            }
        }

        public bool IsGoForwardView
        {
            get
            {
                return isGoForwardView;
            }
            set
            {
                isGoForwardView = value;
                OnPropertyChanged("IsGoForwardView");
            }
        }

        public bool IsFullScreenCommandVisible
        {
            get
            {
                return isFullScreenCommandVisible;
            }
            set
            {
                isFullScreenCommandVisible = value;
                OnPropertyChanged("IsFullScreenCommandVisible");
            }
        }

        public bool ViewLogOut
        {
            get
            {
                return viewLogOut;
            }
            set
            {
                viewLogOut = value;
                OnPropertyChanged("ViewLogOut");
            }
        }

        #endregion

        #region Command

        public DelegatingCommand ForwardViewCommand
        {
            get;
            private set;
        }

        public DelegatingCommand BackwardViewCommand
        {
            get;
            private set;
        }

        public DelegatingCommand HomeViewCommand
        {
            get;
            private set;
        }

        public DelegatingCommand AboutCommand
        {
            get;
            private set;
        }

        public DelegatingCommand FullScreenCommand
        {
            get;
            private set;
        }

        public DelegatingCommand LogOutCommand
        {
            get;
            private set;
        }

        public DelegatingCommand SettingCommand
        {
            get;
            private set;
        }

        #endregion

        public MainViewModel(PageHomeViewModel pageHomeViewModel,
                             PageMoviesViewModel pageMoviesViewModel,
                             PagePicturesViewModel pagePicturesViewModel,
                             PageBooksViewModel pageBooksViewModel,
                             PageMusicViewModel pageMusicViewModel)
        {

            //Init View
            this.views = new ObservableCollection<PageViewModelBase>();
            this.views.Add(pageHomeViewModel);
            this.views.Add(pageMoviesViewModel);
            this.views.Add(pageMusicViewModel);
            this.views.Add(pagePicturesViewModel);
            this.views.Add(pageBooksViewModel);
            this.CurrentContent = this.views[0];

            //Init Command
            ForwardViewCommand = new DelegatingCommand(OnForwardView, CanForwardView);
            BackwardViewCommand = new DelegatingCommand(OnBackwardView, CanBackwardView);
            HomeViewCommand = new DelegatingCommand(OnHomeView);
            AboutCommand = new DelegatingCommand(OnAbout);
            FullScreenCommand = new DelegatingCommand(OnFullScreen);
            LogOutCommand = new DelegatingCommand(OnLogOut);
            SettingCommand = new DelegatingCommand(OnSetting);

            windowAboutVisualizer = Resolve<IWindowAboutVisualizer>();
            authenticationVisualizer = Resolve<IAuthenticationVisualizer>();
            windowSettingsVisualizer = Resolve<IWindowSettingsVisualizer>();
        }

        #region Private Methods

        /// <summary>
        /// Forward View
        /// </summary>
        private void OnForwardView()
        {
            IsGoForwardView = true;
            int index = this.views.IndexOf(CurrentContent);
            this.CurrentContent = this.views[index + 1];
            RaiseCanExecuteChanged();
        }

        /// <summary>
        /// Control Forward View
        /// </summary>
        /// <returns></returns>
        private bool CanForwardView()
        {
            int index = this.views.IndexOf(CurrentContent);
            return !(index + 1 == views.Count);
        }

        /// <summary>
        /// Backward View
        /// </summary>
        private void OnBackwardView()
        {
            IsGoForwardView = false;
            int index = this.views.IndexOf(CurrentContent);
            this.CurrentContent = this.views[index - 1];
            RaiseCanExecuteChanged();
        }

        /// <summary>
        /// Control Forward View
        /// </summary>
        /// <returns></returns>
        private bool CanBackwardView()
        {
            int index = this.views.IndexOf(CurrentContent);
            return !(index == 0);
        }

        /// <summary>
        /// Home View
        /// </summary>
        private void OnHomeView()
        {
            IsGoForwardView = false;
            this.CurrentContent = this.views[0];
            RaiseCanExecuteChanged();
        }

        /// <summary>
        /// Show About Window
        /// </summary>
        private void OnAbout()
        {
            if (windowAboutVisualizer != null)
            {
                windowAboutVisualizer.Show();
            }
        }

        /// <summary>
        /// Raise can execute
        /// </summary>
        private void RaiseCanExecuteChanged()
        {
            BackwardViewCommand.RaiseCanExecuteChanged();
            ForwardViewCommand.RaiseCanExecuteChanged();
        }

        /// <summary>
        /// Enable/Disable Fullscreen
        /// </summary>
        private void OnFullScreen()
        {
            Application.Current.Host.Content.IsFullScreen =
                           !Application.Current.Host.Content.IsFullScreen;
        }

        /// <summary>
        /// Log Out
        /// </summary>
        private void OnLogOut()
        {
            ViewLogOut = false;
            HomeViewCommand.Execute(null);
            if (authenticationVisualizer != null)
            {
                SendMessage(MediatorMessages.LogOut, SessionMb.CurrentUser);
                authenticationVisualizer.Show();
            }

        }

        /// <summary>
        /// Show Settings View
        /// </summary>
        private void OnSetting()
        {
            if (windowSettingsVisualizer != null)
            {
                SendMessage(MediatorMessages.ShowSettingsView, this);
                windowSettingsVisualizer.Show(OnCloseWindowSettings);
            }
        }

        /// <summary>
        /// On Close Settings View
        /// </summary>
        /// <param name="sender"></param>
        private void OnCloseWindowSettings(object sender)
        {
            SendMessage(MediatorMessages.CloseSettingsView, this);
        }

        #endregion

        /// <summary>
        /// Update Current View
        /// </summary>
        /// <param name="page"></param>
        [MessageMediatorTarget(MediatorMessages.UpdateCurrentView)]
        public void UpdateCurrentView(PageViewModelBase page)
        {
            IsGoForwardView = true;
            this.CurrentContent = this.views[views.IndexOf(page)];
            RaiseCanExecuteChanged();
        }

        /// <summary>
        /// Update Current User
        /// </summary>
        /// <param name="page"></param>
        [MessageMediatorTarget(MediatorMessages.UpdateCurrentUser)]
        public void UpdateCurrentUser(User user)
        {
            ViewLogOut = true;
        }

        /// <summary>
        /// Update View Log Out
        /// </summary>
        /// <param name="val"></param>
        [MessageMediatorTarget(MediatorMessages.UpdateViewLogOut)]
        public void UpdateViewLogOut(bool val)
        {
            ViewLogOut = val;
        }

    }
}
